package com.tware.anexter;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import android.text.format.Time;
import android.util.Log;

public class util
{
	private static final String TAG = "Util";
	private static String RKPATH = "/dev/rknand_sys_storage";

    public static String getDateTime()
    {
		Time tm = new Time();
		tm.setToNow();
		int year = tm.year;
		int mon = tm.month + 1;
		int day = tm.monthDay;
		int hour = tm.hour;
		int min = tm.minute;
		int sec = tm.second;
		
		return year+"-"+mon+"-"+day + " " + hour + ":" + min +":"+sec;
    }
	
	public static boolean fileExists(String path)
	{
		File f = new File(path);
		if(f.exists()/* && f.isFile()*/)
		{
			return true;
		}
		
		return false;
	}
	
	public static boolean isRk()
	{
		return fileExists(RKPATH);
	}
	
    /*
     * set the eMMC Size for each range
     * */
	public static float setEmmcSize(float size)
	{
	    if (size >= 30 && size <=60)
	    	return (float) 32;
	    else if (size >= 16 && size <=18)
	    	return (float) 16;
	    else if(size >= 7 && size <= 10)
	    	return (float)  8;
	    else if (size >=4 && size<=5)
	    	return (float)  4;
	    else if (size>=1 && size <=3)
	    	return (float)  2;
	    else
	    	return 0;
	}
	
	public static String getRealSDPath(){
		List<String> sdPathList = new ArrayList<String>();
		/*
		 * Init for Shuttle Image sd path...
		 * 
		 * */
		sdPathList.add("/mnt/external_sd/");
		sdPathList.add("/mnt/sdcard/external_sd/");
		sdPathList.add("/mnt/sdcard/external_sdcard/");
		sdPathList.add("/mnt/sdcard2/");
		sdPathList.add("/mnt/sdcard/external_storage/sdcard1/");
		sdPathList.add("/storage/sdcard1/"); /* for intel cmpc */
		sdPathList.add("/storage/sdcard0/"); /* for intel cmpc */
		
		int size = sdPathList.size();
		for (int i=0; i<size; i++)
		{
			Log.d(TAG, "path -> "+sdPathList.get(i));
			if(fileExists(sdPathList.get(i)+"sd.flg"))
				return sdPathList.get(i);
		}
		return "NOT_FOUND";
	}	

	public static boolean isIntelPlatform()
	{
		String line = null;
		String strCPUName = null;
        try {
        	BufferedReader r = new BufferedReader(new InputStreamReader( new FileInputStream( new File("/proc/cpuinfo") )),1024);
				line = r.readLine();
				while (!line.startsWith("model name")) line = r.readLine();
				strCPUName = line.substring(line.indexOf(":"));
        }
		catch (IOException e) {
				return false;
		}
        
        if (strCPUName.contains("Intel")){
        	return true;
        }else{
        	return false;
        }
	}
	
	public static boolean haveRoot() { 
        boolean mHaveRoot = false;
		if (!mHaveRoot) {
            int ret = execRootCmdSilent("echo test");
            if (ret != -1) { 
                Log.i(TAG, "have root!"); 
                mHaveRoot = true; 
            } else { 
                Log.i(TAG, "not root!"); 
            } 
        } else { 
            Log.i(TAG, "mHaveRoot = true, have root!"); 
        } 
        return mHaveRoot;
    }
	
	 public static int execRootCmdSilent(String cmd) { 
	        int result = -1; 
	        DataOutputStream dos = null; 
	         
	        try { 
	            Process p = Runtime.getRuntime().exec("su"); 
	            dos = new DataOutputStream(p.getOutputStream()); 
	             
	            Log.i(TAG, cmd); 
	            dos.writeBytes(cmd + "\n"); 
	            dos.flush(); 
	            dos.writeBytes("exit\n"); 
	            dos.flush(); 
	            p.waitFor(); 
	            result = p.exitValue(); 
	        } catch (Exception e) { 
	            e.printStackTrace(); 
	        } finally { 
	            if (dos != null) { 
	                try { 
	                    dos.close(); 
	                } catch (IOException e) { 
	                    e.printStackTrace(); 
	                } 
	            } 
	        } 
	        return result; 
	    }
	
}